<?php use_helper('MyPagination'); ?>
<?php slot('submenu') ?>
<div class="search">
	<strong>Thống kê chung</strong>&nbsp;&nbsp;
	<?php echo link_to('Tổng doanh thu theo ngày', '/alert/dailyDownloadContent'); ?>&nbsp;&nbsp;
	<?php echo link_to('Tổng doanh thu theo tháng', '/alert/monthlyDownloadContent'); ?>&nbsp;&nbsp;
	<?php echo link_to('Cảnh báo Ringtone', '/ringtone_alert/list'); ?>
</div>
<?php end_slot() ?>
<div id="sf_admin_container">

	<div id="sf_admin_content" >
	
		<form id="frmCommon" method="POST" action="commonStatistic" >
			<fieldset>
				<div class="form-row">&nbsp;</div>				
				<div class="form-row">
					<input type="hidden" name="action_type" value="get" id="action_type"/>
					
					<label>Tên lệnh</label>			
					<select name="optStatisticType"  id="optStatisticType" style="width: 300px;"  onchange="cboSQLStringOnChange();">
					
						<?php foreach ( $sqlLists as $sql): ?>
								<?php if ($sql->getId() == $optValue) :?>
									<?php $sqlString = $sql->getSqlString(); ?>
									<option selected="selected" value="<?php echo $sql->getId(); ?>"> <?php echo $sql->getName();?> </option>
								<?php else :?>
									<option value="<?php echo $sql->getId(); ?>"> <?php echo $sql->getName();?> </option>
								<?php endif; ?>
						<?php endforeach; ?>
						
					</select>
				</div>
				
				<?php $keys = array(89, 90, 92, 94, 103); ?>					
					<?php if (in_array($optValue, $keys)) :?>   
                                    <?php                    
                                        $value1 = isset($startDate) ? $startDate : null;
                                        $value2 = isset($endDate) ? $endDate : null;
                                        $options = array (
                                                                  'rich' => true,                                                                  
                                                                  'calendar_button_img' => '/sf/sf_admin/images/date.png',
                                                                );
                                        $options = _parse_attributes($options);
                                        $before = _get_option($options, 'before', '');
                                        $middle = _get_option($options, 'middle', '');
                                        $after  = _get_option($options, 'after', '');
                                    ?>
                                    <div class="form-row">
                                    <label>Từ ngày</label>
                                    <?php
                                        echo $before. input_date_tag('start_date',$value1 , $options). $middle;
                                    ?>                                    
                                 </div>
                                <div class="form-row">
                                    <label>Đến ngày</label>
                                    <?php
                                        echo input_date_tag('end_date', $value2, $options). $after;
                                    ?>  
                                 </div>          
					<?php endif; ?>
					
					
					
				<?php if(SF_ENVIRONMENT == 'dev'): ?>
				<div class="form-row">
					<label>Mã lệnh</label>
					<div>
						<textarea id="sqlString"   name="sqlString"   style="width:500px; height:150px" ><?php echo $sqlString;?></textarea>
					</div>
				</div>
				
				<div class="form-row" style="clear:both; margin-left: 122px">
					<?php echo submit_tag('Thực hiện lại', 'style="width:80px"') ?>
					<?php 
                                            echo submit_tag('Lưu lại', 'style="width:80px" onclick="btnSaveAsSQL_OnClick();"');
                                            ?>
						với tên mới là <input type="text" id="asName" name="asName">
				 		* ( Để trắng nếu muốn giữ nguyên tên )
				</div>
                                <?php else: ?>
                                <div class="form-row" style="clear:both; margin-left: 144px; margin-top: 5px">
					<?php echo submit_tag('Thực hiện lại', 'style="width:90px;"') ?>
				</div>
				<?php endif; ?>
			</fieldset>
		</form>	
	
		<label>Kết quả thực hiện</label>
		<div  id="result_search">					
			<?php if ( $permission == false):?>
				Bạn không có quyền thực hiện lệnh này"
			<?php else: ?>				
				<?php if ($result): ?>				  
				  	<strong><?php echo $result->getRecordCount();?> bản ghi</strong>
				  
				  <table class="sf_admin_list"  cellspacing="0">
				  	<thead> </thead>
				  	<tbody>
                                        <?php   for ( $j =0; $j< count($columns); $j ++){
                                                    echo "<th>$columns[$j]</th>";
                                                }
                                        ?>
					<?php  $i = 1; foreach ($result as $row): $odd = fmod(++$i, 2) ?>
						<tr class="sf_admin_row_<?php echo $odd ?>">									
							<?php
                                                        
								$totalCol =  sizeof($row);
								for ( $j =1; $j<= $totalCol; $j ++)
								{		
									if ($optValue ==103) {
	                                	echo ' <td>'.$result->get($j).'% </td>';
									} else {
										echo ' <td>'.$result->get($j).' </td>';
									}
								}
							?>
						</tr>
						
					<?php endforeach; ?>
					</tbody>		
					<tfoot></tfoot>		
				</table>
				<?php endif; ?>
			<?php endif; ?>
			
		</div>
	</div>
</div>
<?php if ($totalPage > 1): ?>
    <div class= "clearfix" align="right">
            <div class="paging_comment" >                    
                    <?php
                        //$url = '/alert/commonStatistic?optStatisticType='.$optValue.'&page=%s';
                        $url = '/alert/commonStatistic?optStatisticType='.$optValue;
                        $url .= (null != $startDate) ? '&start_date='.$startDate : '';
                        $url .= (null != $endDate) ? '&end_date='.$endDate : '';                        
                        $url .='&page=%s';   
                        echo showPage_1($totalPage, $page, $url);
                    ?>
            </div>
    </div>
<?php endif; ?>
<script language="Javascript">
function cboSQLStringOnChange(){
	frm = document.getElementById('frmCommon');
	frm.submit();
}

function btnSaveAsSQL_OnClick(){
	actionType = document.getElementById('action_type');
	actionType.value = "update";
}


</script>
	